﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.Linq;
using System.Web.UI;
using System.Web.UI.WebControls;
using Module;
using Module.QuanLyDuoc.HeThongNhapXuat;

namespace HospitalManager.QuanLyDuoc.HeThongTaiChinh
{
    public partial class ThanhToanHoaDon2 : Page
    {
        private const int PageSize = 20;

        private readonly SelectAllClass _selectAllClass = new SelectAllClass();

        private readonly PhieuNhapVatTu _phieuNhapVatTu = new PhieuNhapVatTu();

        private readonly PhieuTraNhaCungCap _phieuTraNhaCungCap = new PhieuTraNhaCungCap();

        private readonly PhieuHuy _phieuHuy = new PhieuHuy();

        private readonly PhieuXuatBanLe _phieuXuatBanLe = new PhieuXuatBanLe();

        private readonly PhieuXuatKhac _phieuXuatKhac = new PhieuXuatKhac();

        private readonly PhieuYeuCauXuatVatTu _phieuYeuCauXuatVatTu = new PhieuYeuCauXuatVatTu();

        private int _totalrow;

        protected void Page_Load(object sender, EventArgs e)
        {
            var loaiphieu = Request.QueryString["loaiphieu"];
            ClientScript.RegisterClientScriptBlock(typeof(string), "alert", "$(function(){$('#" + loaiphieu + "').addClass('active')});", true);
            if (IsPostBack) return;
            if (Session["username"] == null)
            {
                Response.RedirectToRoute("DangNhap");
            }
            txtTuNgay.Text = DateTime.Now.ToString("dd-MM-yyyy");
            txtDenNgay.Text = DateTime.Now.ToString("dd-MM-yyyy");
            LoadKhoByKhoa();
            LoadPage();
        }

        private void LoadKhoByKhoa()
        {
            ddlKho_Khoa.DataSource = Session["superadmin"].ToString().Equals("true")
                ? _selectAllClass.SelectAllKho()
                : _selectAllClass.SelectKhoByKhoa(Session["makhoa"].ToString());
            ddlKho_Khoa.DataTextField = "TenKho";
            ddlKho_Khoa.DataValueField = "MaKho";
            ddlKho_Khoa.DataBind();
        }

        private void LoadPage()
        {
            var tungay =
                Convert.ToDateTime(DateTime.ParseExact(txtTuNgay.Text, "dd-MM-yyyy", null).ToString("yyyy-MM-dd"));
            var denngay =
                Convert.ToDateTime(DateTime.ParseExact(txtDenNgay.Text, "dd-MM-yyyy", null).ToString("yyyy-MM-dd"));
            var loaiphieu = Request.QueryString["loaiphieu"];
            if (loaiphieu.Equals("li_phieunhapvattu"))
            {
                gvPhieuNhapVatTu.Visible = true;
                gvPhieuTraNhaCungCap.Visible = false;
                gvPhieuXuatBanLe.Visible = false;
                gvPhieuHuy.Visible = false;
                gvPhieuXuatKhac.Visible = false;
                gvPhieuYeuCauXuatVatTu.Visible = false;
                gvPhieuNhapVatTu.DataSource = _phieuNhapVatTu.PhieuNhapVatTu_SelectAll(tungay, denngay,
                    ddlKho_Khoa.SelectedValue, Convert.ToInt32(hfPageIndex.Value.Trim()),
                    PageSize, out _totalrow).AsEnumerable().Where(x => x.Field<Byte>("IsXetDuyet") == 1 || x.Field<Byte>("IsXetDuyet") == 2).Select(x=>x).AsDataView();
                gvPhieuNhapVatTu.DataBind();
            }
            //else if (loaiphieu.Equals("3"))
            //{
            //    gvPhieuNhapVatTu.Visible = false;
            //    gvPhieuTraNhaCungCap.Visible = true;
            //    gvPhieuXuatBanLe.Visible = false;
            //    gvPhieuHuy.Visible = false;
            //    gvPhieuXuatKhac.Visible = false;
            //    gvPhieuYeuCauXuatVatTu.Visible = false;
            //    gvPhieuTraNhaCungCap.DataSource = _phieuTraNhaCungCap.PhieuTraNhaCungCap_SelectAll(tungay, denngay,
            //        Convert.ToBoolean(ddlTrangThaiDuyet.SelectedValue), ddlKho_Khoa.SelectedValue, Convert.ToInt32(hfPageIndex.Value.Trim()),
            //        PageSize, out _totalrow);
            //    gvPhieuTraNhaCungCap.DataBind();
            //}
            //else if (loaiphieu.Equals("5"))
            //{
            //    gvPhieuNhapVatTu.Visible = false;
            //    gvPhieuTraNhaCungCap.Visible = false;
            //    gvPhieuXuatBanLe.Visible = true;
            //    gvPhieuHuy.Visible = false;
            //    gvPhieuXuatKhac.Visible = false;
            //    gvPhieuYeuCauXuatVatTu.Visible = false;
            //    gvPhieuXuatBanLe.DataSource = _phieuXuatBanLe.PhieuXuatBanLe_SelectAll(tungay, denngay,
            //        Convert.ToBoolean(ddlTrangThaiDuyet.SelectedValue), ddlKho_Khoa.SelectedValue, Convert.ToInt32(hfPageIndex.Value.Trim()),
            //        PageSize, out _totalrow);
            //    gvPhieuXuatBanLe.DataBind();
            //}
            //else if (loaiphieu.Equals("6"))
            //{
            //    gvPhieuNhapVatTu.Visible = false;
            //    gvPhieuTraNhaCungCap.Visible = false;
            //    gvPhieuXuatBanLe.Visible = false;
            //    gvPhieuHuy.Visible = true;
            //    gvPhieuXuatKhac.Visible = false;
            //    gvPhieuYeuCauXuatVatTu.Visible = false;
            //    gvPhieuHuy.DataSource = _phieuHuy.PhieuHuy_SelectAll(tungay, denngay,
            //        Convert.ToBoolean(ddlTrangThaiDuyet.SelectedValue), ddlKho_Khoa.SelectedValue, Convert.ToInt32(hfPageIndex.Value.Trim()),
            //        PageSize, out _totalrow);
            //    gvPhieuHuy.DataBind();
            //}
            //else if (loaiphieu.Equals("7"))
            //{
            //    gvPhieuNhapVatTu.Visible = false;
            //    gvPhieuTraNhaCungCap.Visible = false;
            //    gvPhieuXuatBanLe.Visible = false;
            //    gvPhieuHuy.Visible = false;
            //    gvPhieuXuatKhac.Visible = true;
            //    gvPhieuYeuCauXuatVatTu.Visible = false;
            //    gvPhieuXuatKhac.DataSource = _phieuXuatKhac.PhieuXuatKhac_SelectAll(tungay, denngay,
            //        Convert.ToBoolean(ddlTrangThaiDuyet.SelectedValue), ddlKho_Khoa.SelectedValue, Convert.ToInt32(hfPageIndex.Value.Trim()),
            //        PageSize, out _totalrow);
            //    gvPhieuXuatKhac.DataBind();
            //}
            //else if (loaiphieu.Equals("8"))
            //{
            //    gvPhieuNhapVatTu.Visible = false;
            //    gvPhieuTraNhaCungCap.Visible = false;
            //    gvPhieuXuatBanLe.Visible = false;
            //    gvPhieuHuy.Visible = false;
            //    gvPhieuXuatKhac.Visible = false;
            //    gvPhieuYeuCauXuatVatTu.Visible = true;
            //    gvPhieuYeuCauXuatVatTu.DataSource = _phieuYeuCauXuatVatTu.PhieuYeuCauXuatVatTu_SelectAll(tungay, denngay,
            //        Convert.ToBoolean(ddlTrangThaiDuyet.SelectedValue), ddlKho_Khoa.SelectedValue, Convert.ToInt32(hfPageIndex.Value.Trim()),
            //        PageSize, out _totalrow);
            //    gvPhieuYeuCauXuatVatTu.DataBind();
            //}
            GeneratePager(_totalrow, PageSize, Convert.ToInt32(hfPageIndex.Value.Trim()));
        }

        protected void dlPager_ItemCommand(object source, DataListCommandEventArgs e)
        {
            if (e.CommandName != "PageNo") return;
            hfPageIndex.Value = e.CommandArgument.ToString();
            LoadPage();
            e.Item.Attributes.Add("disabled", "disabled");
        }

        public void GeneratePager(int totalRowCount, int pageSize, int currentPage)
        {
            const int totalLinkInPage = 4;
            var totalPageCount = (int)Math.Ceiling((decimal)totalRowCount / pageSize);

            var startPageLink = Math.Max(currentPage - (int)Math.Floor((decimal)totalLinkInPage / 2), 1);
            var lastPageLink = Math.Min(startPageLink + totalLinkInPage - 1, totalPageCount);

            if ((startPageLink + totalLinkInPage - 1) > totalPageCount)
            {
                lastPageLink = Math.Min(currentPage + (int)Math.Floor((decimal)totalLinkInPage / 2), totalPageCount);
                startPageLink = Math.Max(lastPageLink - totalLinkInPage + 1, 1);
            }

            var pageLinkContainer = new List<ListItem>();

            if (startPageLink != 1)
            {
                pageLinkContainer.Add(new ListItem("«", "1", currentPage != 1));
            }
            for (var i = startPageLink; i <= lastPageLink; i++)
            {
                pageLinkContainer.Add(new ListItem(i.ToString(CultureInfo.InvariantCulture),
                    i.ToString(CultureInfo.InvariantCulture), currentPage != i));
            }
            if (lastPageLink != totalPageCount)
                pageLinkContainer.Add(new ListItem("»", totalPageCount.ToString(CultureInfo.InvariantCulture),
                    currentPage != totalPageCount));

            dlPager.DataSource = pageLinkContainer;
            dlPager.DataBind();
        }

        protected void btnTimKiem_Click(object sender, EventArgs e)
        {
            hfPageIndex.Value = "1";
            LoadPage();
        }

        protected void btnDuyetPhieuNhapVatTu_Click(object sender, EventArgs e)
        {
            if (Session["username"] == null)
            {
                Response.RedirectToRoute("DangNhap");
            }
            else
            {
                var maphieunhap = ((LinkButton)sender).CommandArgument;
                //_phieuNhapVatTu.PhieuNhapVatTu_UpdateDuyetPhieu(maphieunhap, Session["username"].ToString(), (int)Application["DaDuyet"]);
                LoadPage();
            }
        }

        protected void btnTraLaiPhieuNhapVatTu_Click(object sender, EventArgs e)
        {
            if (Session["username"] == null)
            {
                Response.RedirectToRoute("DangNhap");
            }
            else
            {
                var maphieunhap = ((LinkButton)sender).CommandArgument;
                //_phieuNhapVatTu.PhieuNhapVatTu_UpdateDuyetPhieu(maphieunhap, Session["username"].ToString(), (int)Application["ChuaDuyet"]);
                LoadPage();
            }
        }

        protected void gvPhieuNhapVatTu_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType != DataControlRowType.DataRow) return;
            var status = Convert.ToInt32(((DataRowView)e.Row.DataItem)["IsXetDuyet"].ToString());
            if (status != 2) return;
            e.Row.Cells[7].FindControl("btnDuyetPhieuNhapVatTu").Visible = false;
            e.Row.Cells[7].FindControl("btnTraLaiPhieuNhapVatTu").Visible = false;
        }

        protected void gvPhieuTraNhaCungCap_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType != DataControlRowType.DataRow ||
                (e.Row.RowState != DataControlRowState.Normal && e.Row.RowState != DataControlRowState.Alternate))
                return;
            e.Row.Cells[9].FindControl("btnDuyetPhieu").Visible = false;
            e.Row.Cells[9].FindControl("btnXoaPhieu").Visible = false;
        }

        protected void btnDuyetPhieu_Click(object sender, EventArgs e)
        {
            if (Session["username"] == null)
            {
                Response.RedirectToRoute("DangNhap");
            }
            else
            {
                var maphieu = ((LinkButton)sender).CommandArgument;
                //_phieuTraNhaCungCap.PhieuTraNhaCungCap_UpdateDuyetPhieu(maphieu, Session["username"].ToString());
                LoadPage();
            }
        }

        protected void btnXoaPhieu_Click(object sender, EventArgs e)
        {
            if (Session["username"] == null)
            {
                Response.RedirectToRoute("DangNhap");
            }
            else
            {
                var maphieu = ((LinkButton)sender).CommandArgument;
                _phieuTraNhaCungCap.PhieuTraNhaCungCap_Delete(maphieu);
                LoadPage();
            }
        }

        protected void gvPhieuHuy_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType != DataControlRowType.DataRow ||
                (e.Row.RowState != DataControlRowState.Normal && e.Row.RowState != DataControlRowState.Alternate))
                return;
            e.Row.Cells[7].FindControl("btnDuyetPhieuHuy").Visible = false;
            e.Row.Cells[7].FindControl("btnXoaPhieuHuy").Visible = false;
        }

        protected void btnDuyetPhieuHuy_Click(object sender, EventArgs e)
        {
            if (Session["username"] == null)
            {
                Response.RedirectToRoute("DangNhap");
            }
            else
            {
                var maphieu = ((LinkButton)sender).CommandArgument;
                //_phieuHuy.PhieuHuy_UpdateDuyetPhieu(maphieu, Session["username"].ToString());
                LoadPage();
            }
        }

        protected void btnXoaPhieuHuy_Click(object sender, EventArgs e)
        {
            if (Session["username"] == null)
            {
                Response.RedirectToRoute("DangNhap");
            }
            else
            {
                var maphieu = ((LinkButton)sender).CommandArgument;
                _phieuHuy.PhieuHuy_Delete(maphieu);
                LoadPage();
            }
        }

        protected void gvPhieuXuatBanLe_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType != DataControlRowType.DataRow ||
               (e.Row.RowState != DataControlRowState.Normal && e.Row.RowState != DataControlRowState.Alternate))
                return;
            e.Row.Cells[7].FindControl("btnDuyetPhieuXuatBanLe").Visible = false;
            e.Row.Cells[7].FindControl("btnXoaPhieuXuatBanLe").Visible = false;
        }

        protected void btnDuyetPhieuXuatBanLe_Click(object sender, EventArgs e)
        {
            if (Session["username"] == null)
            {
                Response.RedirectToRoute("DangNhap");
            }
            else
            {
                var maphieu = ((LinkButton)sender).CommandArgument;
                _phieuXuatBanLe.PhieuXuatBanLe_UpdateDuyetPhieu(maphieu, Session["username"].ToString(), (int)Application["DaDuyet"],"");
                LoadPage();
            }
        }

        protected void btnXoaPhieuXuatBanLe_Click(object sender, EventArgs e)
        {
            if (Session["username"] == null)
            {
                Response.RedirectToRoute("DangNhap");
            }
            else
            {
                var maphieu = ((LinkButton)sender).CommandArgument;
                _phieuXuatBanLe.PhieuXuatBanLe_Delete(maphieu);
                LoadPage();
            }
        }

        protected void gvPhieuXuatKhac_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType != DataControlRowType.DataRow ||
               (e.Row.RowState != DataControlRowState.Normal && e.Row.RowState != DataControlRowState.Alternate))
                return;
            e.Row.Cells[7].FindControl("btnDuyetPhieuXuatKhac").Visible = false;
            e.Row.Cells[7].FindControl("btnXoaPhieuXuatKhac").Visible = false;
        }

        protected void btnDuyetPhieuXuatKhac_Click(object sender, EventArgs e)
        {
            if (Session["username"] == null)
            {
                Response.RedirectToRoute("DangNhap");
            }
            else
            {
                var maphieu = ((LinkButton)sender).CommandArgument;
                //_phieuXuatKhac.PhieuXuatKhac_UpdateDuyetPhieu(maphieu, Session["username"].ToString());
                LoadPage();
            }
        }

        protected void btnXoaPhieuXuatKhac_Click(object sender, EventArgs e)
        {
            if (Session["username"] == null)
            {
                Response.RedirectToRoute("DangNhap");
            }
            else
            {
                var maphieu = ((LinkButton)sender).CommandArgument;
                _phieuXuatKhac.PhieuXuatKhac_Delete(maphieu);
                LoadPage();
            }
        }

        protected void gvPhieuYeuCauXuatVatTu_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType != DataControlRowType.DataRow ||
               (e.Row.RowState != DataControlRowState.Normal && e.Row.RowState != DataControlRowState.Alternate))
                return;
            e.Row.Cells[6].FindControl("btnDuyetPhieuYeuCauXuatVatTu").Visible = false;
            e.Row.Cells[6].FindControl("btnXoaPhieuYeuCauXuatVatTu").Visible = false;
        }

        protected void btnDuyetPhieuYeuCauXuatVatTu_Click(object sender, EventArgs e)
        {
            if (Session["username"] == null)
            {
                Response.RedirectToRoute("DangNhap");
            }
            else
            {
                var maphieu = ((LinkButton)sender).CommandArgument;
                //_phieuYeuCauXuatVatTu.PhieuYeuCauXuatVatTu_UpdateDuyetPhieu(maphieu, Session["username"].ToString());
                LoadPage();
            }
        }

        protected void btnXoaPhieuYeuCauXuatVatTu_Click(object sender, EventArgs e)
        {
            if (Session["username"] == null)
            {
                Response.RedirectToRoute("DangNhap");
            }
            else
            {
                var maphieu = ((LinkButton)sender).CommandArgument;
                _phieuYeuCauXuatVatTu.PhieuYeuCauXuatVatTu_Delete(maphieu);
                LoadPage();
            }
        }
    }
}